package com.loie.xcx.task;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.mapper.FkblMapper;
import com.loie.xcx.model.bankdto.Shbank_OrdrRgst;
import com.loie.xcx.model.dto.ApplyPaymentDTO;
import com.loie.xcx.service.ApprovalformService;
import com.loie.xcx.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;

/**
 * @author zhl
 */
@Configuration
@EnableScheduling
@Slf4j
public class SyncPermissionTask {
    @Autowired
    ApprovalformService approvalformService;

    @Autowired
    private
    FkblMapper fkblMapper;
    private static final Logger paramLogger = LoggerFactory.getLogger("banklog");



    @Scheduled(fixedDelay = 1000*60*60)
    private void getshbankzt() throws Exception {
        List<Shbank_OrdrRgst> shbankOrdrRgstList=fkblMapper.getshbank_ddzt("");
        String url = "http://127.0.0.1:8090/QianchengAPI/SHAPI/SendToSpdb/queryAssetStatus";
        paramLogger.info("********更新上银保理的订单状态**********");
        if (shbankOrdrRgstList.size()==0){
            paramLogger.info("无相应未上链或未放款的记录");
        }
        for (Shbank_OrdrRgst shbank_ordrRgst:shbankOrdrRgstList){
            paramLogger.info(String.format("上银保理发票号:%s-供应商名称:%s-原始资产编号:%s获取订单审核状态",
                    shbank_ordrRgst.getContractNo(),shbank_ordrRgst.getSpyCompanyName(),
                    shbank_ordrRgst.getSourceAssetNo()));
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("sourceAssetNo", shbank_ordrRgst.getSourceAssetNo());
            //jsonObject.put("corpserialnos","AU062");
            HashMap<String, String> header = new HashMap<>();

            header.put("msg", jsonObject.toJSONString());
            String response = HttpUtil.httpAjaxPost(url, header, false);



        }
        //*******取到期的上银保理********
        paramLogger.info("********更新到期应付的上银保理的付款申请**********");
        List<ApplyPaymentDTO> applyPaymentDTOList=fkblMapper.getApplyno();
        if (applyPaymentDTOList.size()==0){
            paramLogger.info("无相应到期的上银保理的付款申请记录");
        }
        if (applyPaymentDTOList.size()>0){
            for (ApplyPaymentDTO applyPaymentDTO:applyPaymentDTOList){
                applyPaymentDTO.setPaymentBank("上海银行东部新城支行");
                applyPaymentDTO.setBankAccount("03004507826");
                applyPaymentDTO.setHqkpr("");

                fkblMapper.update_applypayment_fkzt(applyPaymentDTO);
                paramLogger.info(String.format("付款单号:%s-发票号:%s的上银保理应付到期",
                        applyPaymentDTO.getCONTID(),applyPaymentDTO.getApplyNo()));
            }
        }





    }
//    @Scheduled(cron =  "0 30 9,13,17 * * ?")
//    @Scheduled(cron = "0 0/15 * * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
//    private void sendmail() {
//
//        JsonResult<JSONObject> mailist=approvalformService.sendmiallist();
//        //JSONObject jsonObject=JSON.toJSONString(mailist);
//        JSONObject jsonObject =  JSON.parseObject(JSON.toJSONString(mailist));
//        if (jsonObject.containsKey("success")&&jsonObject.getString("success").equals("true")){
//            log.info("*************审批意见邮件处理开始********");
//            log.info(jsonObject.toJSONString());
//            log.info("**********审批意见邮件处理结束*********");
//        }
//        else
//            {log.info(jsonObject.toJSONString());}
//        //log.info(JSON.toJSONString(mailist));
//
//
//
//    }
}
